package com.yicong.ants.ui.video;

import android.content.pm.ActivityInfo;

import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.fragment.BaseStatefulFragment;
import com.dueeeke.videoplayer.player.VideoView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.FragmentVideoBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.ui.video.adapter.VideoListAdapter;
import com.yicong.ants.utils.StatusBarUtil;
import com.yicong.ants.utils.video.cache.PreloadManager;
import com.yicong.ants.utils.video.cache.ProxyVideoCacheManager;

/**
 * 短视频列表刷
 */
public class VideoListFragment extends BaseStatefulFragment<FragmentVideoBinding> {
    RecyclerView mRecyclerView;
    VideoListAdapter mAdapter;
    VideoView mVideoView;
    int mPageIndex;

    /**
     * 当前播放的位置
     */
    protected int mCurPos = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initEventAndData() {
        StatusBarUtil.setStatusImmerAndTextWhite(getActivity().getWindow());
        // 下拉刷新
        mRecyclerView = mDataBind.rvTiktok;
        mDataBind.swipeRefresh.setOnRefreshListener(() -> {
            BusinessHelper.uploadVids(mDisposable);
            RxHelper.timerConsumer(2000, aLong -> mAdapter.loadPageData(1));
        });

        initAdapter();
        onLoadData();
    }

    public VideoListFragment setPageIndex(int pageIndex) {
        mPageIndex = pageIndex;
        return this;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mIsVisible) {
            onLoadData();
        }

        if (mVideoView == null) {
            return;
        }
        if (mIsVisible) {
            mVideoView.resume();//继续播放

        } else {
            mVideoView.pause();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (mVideoView == null) {
            return;
        }
        if (mIsHidden) {
            mVideoView.pause();
        }
    }

    @Override
    protected void onLoadData() {
        // 避免关注页没选中就加载，所以做这些操作去判断
        if (VideoMainFragment.mCurPage != mPageIndex) {
            return;
        }

        if (mAdapter == null) {
            return;
        }
        if (CollectionHelper.isNotEmpty(mAdapter.getData())) {
            return;
        }

        mAdapter.loadPageData(1);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Refresh_Video_List:
                mDataBind.swipeRefresh.setRefreshing(false);
                break;
            case Constants.Event.Expect_Refresh_Dashboard:
                onLoadData();
                break;
        }
    }

    void initAdapter() {
        mAdapter = new VideoListAdapter(this, getActivity(), mDisposable);
        mAdapter.mRecommend = Math.abs(mPageIndex - 1);
        mAdapter.bindToRecyclerView(mRecyclerView);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(mAdapter.getViewPagerLayoutManager());
        mVideoView = mAdapter.mVideoView;
    }

    boolean isNotFocus() {
        return VideoMainFragment.mCurPage != mPageIndex;
    }

    private void releaseVideoView() {
        mVideoView.release();
        if (mVideoView.isFullScreen()) {
            mVideoView.stopFullScreen();
        }
        if (getActivity().getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        mCurPos = -1;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        releaseVideoView();
        PreloadManager.getInstance(mContext).removeAllPreloadTask();
        //清除缓存，实际使用可以不需要清除，这里为了方便测试
        if (AppHelper.isDebug()) {
            ProxyVideoCacheManager.clearAllCache(mContext);
        }
    }
}
